﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace CommonLibrary.Common
{
    public static class FileToHash
    {
        /// <summary>
        /// 获取文件对应的hash值
        /// </summary>
        /// <param name="fs"></param>
        /// <returns></returns>
        public static string GetFileHashName(this Stream fs)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] byt = md5.ComputeHash(fs);
            string hashcode = BitConverter.ToString(byt);
            hashcode = hashcode.Replace("-", "");

            return hashcode;

            //using (HashAlgorithm hashAlg = HashAlgorithm.Create())
            //{
            //    byte[] hashBytes = hashAlg.ComputeHash(fs);
            //    string hashfilename = "";
            //    foreach (byte bit in hashBytes)
            //    {
            //        hashfilename += bit;
            //    }

            //    return hashfilename;
            //}
        }
    }
}
